<template>
  <el-container>
      <!-- 头部 -->
    <el-header>
      <div>
        <img src="https://picsum.photos/seed/picsum/40/40" alt="">
        <span>购物商城管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 下部 -->
    <el-container>
        <!-- 左侧边栏 -->
      <el-aside :width=" isMegeSubmenu ? '64px' : '200px'">
        <div class="toggle-button" @click="megeSubmenu">|||</div>
          <!-- 左边菜单栏 -->
        <el-menu
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409bff"
        unique-opened
        :collapse="isMegeSubmenu"
        :collapse-transition="false"
        router
        :default-active="activePath">
        <!-- 一级菜单 -->
            <el-submenu v-bind:index="submenu.id+''" v-for="submenu in menuList" :key="submenu.id">
                <!-- 一级菜单区域 -->
                <template slot="title">
                    <!-- 图标 -->
                    <i :class="submenuIcons[submenu.id]"></i>
                    <!-- 文本 -->
                    <span>{{submenu.authName}}</span>
                </template>
                <!-- 二级菜单 -->
                <el-menu-item :index="'/'+twoMenu.path" @click="savePath('/'+twoMenu.path)" v-for="twoMenu in submenu.children" :key="twoMenu.id">
                    <i class="el-icon-menu"></i>
                    <span>{{twoMenu.authName}}</span>
                </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      submenuIcons: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujubaobiao'
      },
      // 合并菜单子项
      isMegeSubmenu: false,
      // activePath
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // Home.vue刷新的时候激活path也会 变为''
    this.activePath = window.sessionStorage.getItem('path')
  },
  methods: {
    loginOut: function () {
      // 退出1.清除token 2.跳转登陆页
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) this.$message.error(res.meta.msg)
      console.log(res.data)
      this.menuList = res.data
    },
    megeSubmenu: function () {
      this.isMegeSubmenu = !this.isMegeSubmenu
    },
    savePath: function (twoPath) {
      window.sessionStorage.setItem('path', twoPath)
      this.activePath = twoPath
    }
  }

}
</script>

<style lang="less" scoped>
.el-container{
    height: 100%;
}
.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #373d41;
}
.toggle-button {
    color: #fff;
    text-align: center;
    font-size: 20px;
    line-height: 24px;
    // background-color: #eee;
}
.el-menu {
    border-right: none;
}
.iconfont {
  margin-right: 15px;
}
.el-main{
  background-color: #eee;
}

</style>
